package leetcode_901_1000;

import java.util.*;

public class LeeCode_966 {
    public static void main(String[] args) {

    }
    private static String[] spellchecker(String[] wordlist, String[] queries) {
        Set<String> used = new HashSet<>();
        Map<String, String> map = new HashMap<>();
        Set<String> set = new HashSet<>();
        Map<String, String> metas = new HashMap<>();
        Set<Character> yuan = new HashSet<>();
        yuan.add('a');
        yuan.add('e');
        yuan.add('i');
        yuan.add('o');
        yuan.add('u');
        for (String word : wordlist) {
            set.add(word);
            String key = getSmall(word);
            if (used.contains(key))
                continue;
            used.add(key);
            if (!map.containsKey(key)){
                map.put(key, word);
            }
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < key.length(); j++) {
                if (yuan.contains(key.charAt(j))){
                    list.add(j);
                }
            }
            if (list.size() > 0) {
                dfs(metas, key, list, 0, word);
            }
        }
        String[] ans = new String[queries.length];
        for (int i = 0; i < ans.length; i++) {
            if (set.contains(queries[i])){
                ans[i] = queries[i];
                continue;
            }
            String key = getSmall(queries[i]);
            if (map.containsKey(key)){
                ans[i] = map.get(key);
                continue;
            }
            if (metas.containsKey(key)) {
                ans[i] = metas.get(key);
                continue;
            }
            ans[i] = "";
        }
        return ans;
    }

    private static void dfs(Map<String, String> metas, String str, List<Integer> is, int index, String pre){
        if (index >= is.size()){
            if (!metas.containsKey(str)){
                metas.put(str, pre);
            }
            return;
        }
        char[] tab = new char[]{'a', 'e', 'i', 'o', 'u'};
        for (int i = 0; i < 5; i++) {
            StringBuilder sb = new StringBuilder(str);
            dfs(metas, sb.replace(is.get(index), is.get(index) + 1, tab[i] + "").toString(), is, index + 1, pre);
        }
    }

    private static String getSmall(String word) {
        char[] cs = word.toCharArray();
        int t = 'a' - 'A';
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] >= 'A' && cs[i] <= 'Z'){
                cs[i] += t;
            }
        }
        return new String(cs);
    }
}
